package com.webwork.common.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

/**
 * dao查询条件
 *
 */
public class SqlCondition {
	
	public final static String EQ = " = ";

	public final static String GT = " > ";

	public final static String GTEQ = " >= ";

	public final static String LT = " < ";

	public final static String LTEQ = " <= ";

	public final static String BETWEEN = " BETWEEN ";

	public final static String AND = " AND ";

	public final static String IN = " IN ";
	
	public final static String OR = "OR";
	
	public final static String LIKE = " LIKE ";

	/**
	 * 多个条件的list
	 */
	private List<String> conditionList;

	public List<String> getConditionList() {
		return conditionList;
	}

	public void setConditionList(List<String> conditionList) {
		this.conditionList = conditionList;
	}
	
	/**
	 * add一行条件,使用AND联结
	 * @param condition
	 * @return 
	 */
	public SqlCondition addLineAnd(String condition) {
		if (Objects.isNull(conditionList)) {
			conditionList = new ArrayList<>();
		}
		conditionList.add(condition(AND, condition, "", 0));
		return this;
	}
	
	/**
	 * add一行条件,使用OR联结
	 * @param condition
	 * @return 
	 */
	public SqlCondition addLineOr(String condition) {
		if (Objects.isNull(conditionList)) {
			conditionList = new ArrayList<>();
		}
		conditionList.add(condition(OR, condition, "", 0));
		return this;
	}
	
	/**
	 * add一行条件, 可指定操作符
	 * @param condition
	 * @param operator 操作符
	 */
	public void addLineAnd(String condition, String operator) {
		if (Objects.isNull(conditionList)) {
			conditionList = new ArrayList<>();
		}
		conditionList.add(condition(AND, condition, operator, 1));
	}
			
	/**
	 * or一行条件, 可指定操作符
	 * @param condition
	 * @param operator 操作符
	 */
	public void addLineOr(String condition, String operator) {
		if (Objects.isNull(conditionList)) {
			conditionList = new ArrayList<>();
		}
		conditionList.add(condition(OR, condition, operator, 1));
	}	
	
	/**
	 * 默认使用AND来联结
	 * @param condition
	 * @param operationCode
	 */
//	public void addAnd(String condition) {
//		if (Objects.isNull(conditionList)) {
//			conditionList = new ArrayList<>();
//		}
//		conditionList.add(condition(AND, condition, null, 0));
//	}
	
	/**
	 * 默认使用AND来联结
	 * @param condition
	 * @param operationCode
	 * @param length
	 */
//	public void add(String condition, String operationCode, int length) {
//		if (Objects.isNull(conditionList)) {
//			conditionList = new ArrayList<>();
//		}
//		conditionList.add(condition(AND, condition, operationCode, length));
//	}
	
	/**
	 * add一个条件
	 * @param operationCode1 条件前的操作符(可缺省)
	 * @param condition 不可缺省
	 * @param operationCode2 条件后的操作符(可缺省)
	 */
	public static String condition(String operationCode1, String condition, String operationCode2, int length) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isEmpty(operationCode1)) {
			sb.append(" ").append(operationCode1).append(" ");
		}
		sb.append(condition);
		if (!StringUtils.isEmpty(operationCode2)) {
			sb.append(" ").append(operationCode2).append(" ");
		}
		if (length > 0) {
			for (int i = 0; i < length; i++) {
				sb.append("?,");
			}
			// 删掉最后一个,
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}
	
	/**
	 * and语句,只是把不同条件串联
	 * @param condition
	 * @param operationCode2
	 */
	public static String conditionAnd(String condition) {
		return condition(AND, condition, null, 0);
	}
	
	/**
	 * =语句
	 * @param condition
	 * @return
	 */
//	public static String conditionEquals(String condition) {
//		return condition(AND, condition, EQUALS, 1);
//	}
	
	/**
	 * like语句
	 * @param condition
	 * @return
	 */
//	public static String conditionLike(String condition) {
//		return condition(AND, condition, LIKE, 1);
//	}
	
	/**
	 * and语句
	 * @param condition
	 * @param operationCode2
	 */
//	public static String conditionOr(String condition, String operationCode2) {
//		return condition(OR, condition, operationCode2, 1);
//	}
	
	/**
	 * or语句,只是把不同条件串联
	 * @param condition
	 * @param operationCode2
	 */
	public static String conditionOr(String condition) {
		return condition(OR, condition, null, 0);
	}
	
	/**
	 * 前面没有联结条件
	 * @param condition
	 * @return
	 */
	public static String conditionBetween(String condition) {
		StringBuilder sb = new StringBuilder();
		sb.append(condition(null, condition, BETWEEN, 1));
		sb.append(condition(null, "", AND, 1));
		return sb.toString();
	}
	
	/**
	 * 带有连接条件
	 * @param operationCode1, 用来决定用AND或者OR
	 * @param condition
	 * @return
	 */
	public static String conditionBetween(String operationCode1, String condition) {
		StringBuilder sb = new StringBuilder();
		sb.append(condition(operationCode1, condition, BETWEEN, 1));
		sb.append(condition(null, null, AND, 1));
		return sb.toString();
	}
	
	
	/**
	 * in子查询
	 * @param operationCode1
	 * @param condition
	 * @param length
	 * @param operationCode2
	 */
	public static String conditionIn(String condition, int length) {
		StringBuilder sb = new StringBuilder();
		sb.append(condition(null, condition, IN + "(", length));
		sb.append(")");
		return sb.toString();
	}
	
	/**
	 * 转换成sql语句
	 * @return
	 */
	public StringBuilder toSql() {
		StringBuilder sb = new StringBuilder();
		for (String s : conditionList) {
			sb.append(s);
		}
		return sb;
	}
	
}
